package org.sam.swing.builder;

import java.awt.Component;
import java.awt.Container;

/**
 * 绝对定位布局实现类
 * 
 * @author sam
 *
 */
public class AbsoluteLayoutBuilder extends AbstractFixedLayoutBuilder {

	/**
	 * 绝对定位布局实现类
	 * 
	 * @param parent
	 */
	public AbsoluteLayoutBuilder(Container parent) {
		super(parent);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void init() {
		this.yLine = -1;
		if (this.parent != null) {
			this.parent.setLayout(null);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void append(Component component) {

		if (component == null)
			throw new RuntimeException("input component is null");

		// 暂时实现一个label和一个editor的方式
		int y = this.getAndSetNextYLine();

		// 标签控件
		component.setBounds(this.xLabelLine, y, lblWidth, this.editorHeight);
		this.parent.add(component);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void append(Component lable, Component editor) {
		if (this.parent == null)
			throw new RuntimeException("parent container is null");

		if (lable == null || editor == null)
			throw new RuntimeException("not support components length");

		// 暂时实现一个label和一个editor的方式
		int y = this.getAndSetNextYLine();

		// 标签控件
		lable.setBounds(this.xLabelLine, y, lblWidth, this.editorHeight);
		this.parent.add(lable);

		// 编辑控件
		editor.setBounds(this.xEditorLine, y - offset, lblWidth, this.editorHeight);
		this.parent.add(editor);
	}
}
